package com.ly.springsolrtest.springsolr.service.impl;
import com.ly.springsolrtest.springsolr.bo.ArticleBo;
import com.ly.springsolrtest.springsolr.common.SolrParams;
import com.ly.springsolrtest.springsolr.service.ArticleBoService;
import com.ly.springsolrtest.springsolr.util.SolrUtil;
import org.apache.solr.client.solrj.impl.HttpSolrClient;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;


/**
 * SOLRJ client操作数据方式
 *
 * @param
 * @author linweijian
 * @date 2018/3/30 12:51
 * @return
 */
@Service
public class ArticleBoClientServiceImpl implements ArticleBoService {

   public static final HttpSolrClient solrClient = new HttpSolrClient.Builder("http://localhost:8983/solr/articles").build();

    @Override
    public void save(ArticleBo... articleBos)  {
        SolrUtil.save(solrClient, articleBos);
    }

    @Override
    public void deleteById(String id)  {
        SolrUtil.deleteById(solrClient, id);
    }

    @Override
    public ArticleBo getArticleBoById(String id){
        return SolrUtil.queryById(solrClient, id, ArticleBo.class);
    }

    @Override
    public List<ArticleBo> getHighlight(String keywords, int startIndex, int pageSize) {
        SolrParams solrParams = new SolrParams();
        solrParams.setEnableHighlight(true);
        solrParams.setHighlightFragsize(0);
        solrParams.setQueryString("name:" + keywords + "or content:" + keywords);
        solrParams.setHighlightField(Arrays.asList("name", "content"));
        solrParams.setHighlightSimplePre("<font color='red'>");
        solrParams.setHighlightSimplePost("</font>");
        return SolrUtil.getHighlight(solrClient, solrParams, 0, 10, ArticleBo.class);
    }




}
